/***********************************************************************************
Name:  BookedAccountHomeLoanDetail
Copyright © 2012  Lodha Group
===================================================================================
===================================================================================
Purpose:
-------
Controller for BookedAccountHomeLoanDetail VF page for customer portal Users
===================================================================================
===================================================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          02/05/2012      INITIAL DEVELOPMENT
************************************************************************************/
public class BookedAccountHomeLoanDetail {

    public BookedAccountHomeLoanDetail(ApexPages.StandardController controller) {

    }


    public String accountid{get;set;}
    public String accountname{get;set;}
    public String accountowner{get;set;}
    public String accountrecordtype{get;set;}
    public String coapplicant{get;set;}   
    public String status{get;set;}
    public String mobilephone{get;set;}           
    public String billingaddress{get;set;}
    public String mobile{get;set;}
    public String phone{get;set;}
    public String email{get;set;}        
    public String Instruction  {get;set;}
    public Account a {get;set;}
    public Boolean accountlayout {get;set;}
    public Boolean caselayout {get;set;}
    public Boolean bankdetail{get;set;}   
    public String projectname{get;set;}
    public String cluster{get;set;}
    public String wing{get;set;}
    public String flatno{get;set;}    
    public String considerationvalue {get;set;} 
    public String incomegroup{get;set;}
    public String industry{get;set;}
    public String profile{get;set;}
    public Case newcase{get;set;}
    
    public List<Casewrapper> wrapperlist{get;set;}
    public List<selectOption> incomegrouplist{get;set;}
    public List<selectOption> profilelist{get;set;}
    public List<selectOption> industrylist{get;set;}
    
    public Boolean editmode{get;set;}
    public Boolean displaymode{get;set;}
    
    public Boolean Isbankdetail{get;set;}
    public Boolean Isinteractionpick{get;set;}
    public Boolean Isregistration{get;set;}
    public Boolean Issanctionpick{get;set;}
    public Boolean Ispdcompleted{get;set;}
    public Boolean Isdemandpick{get;set;}
    public Boolean Isdisbursement{get;set;}
    public Boolean Isnotdisbursement{get;set;}
    
    public BookedAccountHomeLoanDetail()
    {
        Initmethod();
    }
    
    public void Initmethod(){
        editmode=false;
        displaymode=true;
        newcase=new Case();
        newcase.Status='New';
                        
        accountid=ApexPages.currentPage().getParameters().get('id');
        accountlayout=true;
        caselayout=false;
        Isbankdetail=false;
        Isinteractionpick=false;
        Isregistration=false;
        Issanctionpick=false;
        Ispdcompleted=false;
        Isdemandpick=false;
        IncomegroupVO();
        IndustryVO();
        ProfileVO();
        Accountdetail();
    
    }
    
    
    public void IncomegroupVO(){    
        incomegrouplist=new List<selectOption>();
        String incomegroup='Income_Group__c';
        incomegrouplist=describepicklist(incomegroup);
    }
    public void IndustryVO(){    
        industrylist=new List<selectOption>();
        String industry='Industry__c';
        industrylist=describepicklist(industry);
    }
    public void ProfileVO(){    
        profilelist=new List<selectOption>();
        String profile='Profile__c';
        profilelist=describepicklist(profile);
    }
    
    public List<selectOption> describepicklist(String fieldname){
        
    List<selectOption> options = new List<selectOption>();
    Schema.sObjectType sobject_type = Account.getSObjectType(); 
    Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); 
    Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();   
    List<Schema.PicklistEntry> pick_list_values = field_map.get(fieldname).getDescribe().getPickListValues();
     
     options.add(new selectOption('None', 'Select'));
        for (Schema.PicklistEntry a : pick_list_values) {     
             options.add(new selectOption(a.getLabel(), a.getValue())); 
          }      
              return options;
    
    }
    
    public void Accountdetail(){
    
        a=[select id,Name,Owner.Name,Co_Applicant__c,Status__c,Mobile_Phone__c,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,
          Mobile1__c,Phone,PersonEmail,Industry__c,Income_Group__c,Profile__c,(select Consideration_Value__c,CLuster__c,Project__c,Wing__c,Flat_No__c from Booking_Details__r),
          (select Id,AccountId,CaseNumber,Subject,Sub_Type__c,Query_Type__c from Cases) from Account where id=:accountid];
           
        accountowner=a.Owner.Name;        
        accountname=a.Name;
        coapplicant=a.Co_Applicant__c;        
        status=a.Status__c;
        mobilephone=a.Mobile_Phone__c;
        incomegroup=a.Income_Group__c;
        industry=a.Industry__c;
        profile=a.profile__c; 
               
        if(a.BillingStreet!=null&&a.BillingStreet!=''){
            billingaddress=a.BillingStreet;
        }
        if(a.BillingCity!=null&&a.BillingCity!=''){
            billingaddress=billingaddress+a.BillingCity;
        }
        if(a.BillingState!=null&&a.BillingState!=''){
            billingaddress=billingaddress+a.BillingState;
        }
        if(a.BillingPostalCode!=null&&a.BillingPostalCode!=''){
            billingaddress=billingaddress+a.BillingPostalCode;
        }
        if(a.BillingCountry!=null&&a.BillingCountry!=''){
            billingaddress=billingaddress+a.BillingCountry;
        }
        
        mobile=a.Mobile1__c;
        phone=a.Phone;
        email=a.PersonEmail;
      
      for(Booking_Details__c  bookingdetails : a.Booking_Details__r){
      
         projectname=bookingdetails.Project__c;
         cluster=bookingdetails.CLuster__c;
         wing=bookingdetails.Wing__c;
         flatno=bookingdetails.Flat_No__c;
      
      }              
        wrapperlist=new List<Casewrapper>();
        for(Case  accountcases : a.Cases)
        {
           Casewrapper wrapper=new Casewrapper();
           wrapper.caseaccountId=accountcases.AccountId;
           wrapper.caseId=accountcases.Id;
           wrapper.casenumber=accountcases.CaseNumber;
           wrapper.subject=accountcases.Subject;
           wrapper.querytype=accountcases.Query_Type__c;
           wrapper.subtype=accountcases.Sub_Type__c;
           wrapperlist.add(wrapper);
        }
    }
    
    
    public pageReference Editaccount(){
    
        editmode=true;
        displaymode=false;
        return null;
    }
     public pageReference Cancelaccountupdate(){
    
        editmode=false;
        displaymode=true;
        return null;
    }
    public void Updateaccount(){
    
        editmode=false;
        displaymode=true;
        Account acc=[Select id,Industry__c,Income_Group__c,Profile__c from Account where id=:accountid];
        acc.Industry__c=industry;
        acc.Income_Group__c=incomegroup;
        acc.Profile__c=profile;
    
        update acc;
    }
    
    
    public class Casewrapper{
    
        public String caseId{get;set;}
        public String caseaccountId{get;set;}
        public String caseaccountname{get;set;}
        public String casenumber{get;set;}
        public String subject{get;set;}
        public String querytype{get;set;}
        public String subtype{get;set;}
    }
    
    
    public pageReference CreateCase(){
        
        accountlayout=false;
        caselayout=true;                
        return null;
    }
     
    
   public void Onchange(){
                            
        if(newcase.Loan_Stages__c=='Lodha Login'||newcase.Loan_Stages__c=='Bank Login'||newcase.Loan_Stages__c=='Disbursement')
         {
            Isbankdetail=true;
            if(newcase.Loan_Stages__c=='Disbursement'){
               Isdisbursement=true;
               Isnotdisbursement=false;
             }else{
                  Isdisbursement=false;
                  Isnotdisbursement=true;
                }
            
         }else{         
           Isbankdetail=false;
         }         
       if(newcase.Loan_Stages__c=='Initial Call'||newcase.Loan_Stages__c=='Appointment'||newcase.Loan_Stages__c=='In Process')
         {
           Isinteractionpick=true;
               
         }else{
             Isinteractionpick=false;
         }
        if(newcase.Loan_Stages__c=='Sanction')
           {
             
             Issanctionpick=true;
           }else{
               Issanctionpick=false;
           }
        if(newcase.Loan_Stages__c=='Registration')
          {
            
            Isregistration=true;
          }else{
            Isregistration=false;
          }
        if(newcase.Loan_Stages__c=='In Process')
          {
             Ispdcompleted=true;
          }else{
             Ispdcompleted=false;
          }
        if(newcase.Loan_Stages__c=='Demand')
          {
              Isdemandpick=true;
          }else{
              Isdemandpick=false;
          }      
   }
   
   public pageReference insertcase(){    
   
        newcase.AccountId=accountid;  
        insert newcase;
       
       pageReference pageref=new pageReference('/apex/BookedAccountHomeLoanDetail?id='+accountid);
        accountlayout=true;
        caselayout=false;
        Accountdetail();
        newcase=null;
        Initmethod();
        return null;
   }
    public pageReference Cancel(){    
    
        accountlayout=true;
        caselayout=false;
        Initmethod();
        return null;
    }
    public static testmethod void testCaseCreatePageController () {
    
        Account a=new Account();
        a.RecordTypeId=[Select id from RecordType where SobjectType='Account' AND Name='Booked Account' Limit 1].Id;
        a.FirstName='Test';
        a.LastName='Account';
        a.Co_Applicant__c='Test Applicant';        
        a.Status__c='Test Status';
        a.Mobile_Phone__c='1101010101';
        a.Income_Group__c='Test Group';
        a.Industry__c='Test Industry';
        a.profile__c='Test Profile';
        a.BillingStreet='Test Street';
        a.BillingPostalCode='1234';
        a.BillingCity='Test City';
        a.BillingState='Test State';
        a.BillingCountry='Test Country';                  
        insert a;
        
        Case c=new Case();
        c.AccountId=a.Id;        
        insert c;
        
        Booking_Details__c bd=new Booking_Details__c();
        bd.Account__c=a.Id;
        bd.Project__c='Test Project';
        bd.CLuster__c='Test Cluster';
        bd.Wing__c='Test Wing';
        bd.Flat_No__c='12345';
        bd.Flat_ID__c='12345';
        bd.CRN__c='12345';
        insert bd;
        
        ApexPages.currentPage().getParameters().put('id',a.id);
        ApexPages.StandardController controller = new ApexPages.StandardController(new Case());
        BookedAccountHomeLoanDetail bookedaccdetail=new BookedAccountHomeLoanDetail();
        bookedaccdetail.Editaccount();
        bookedaccdetail.Cancelaccountupdate();
        bookedaccdetail.Cancel();
        bookedaccdetail.CreateCase();
        bookedaccdetail.newcase.Loan_Stages__c='Disbursement';
        bookedaccdetail.Onchange();
        bookedaccdetail.insertcase();
        bookedaccdetail.Updateaccount();
        
        
       
        
        
    }
}